﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataConvertTool
{
    /// <summary>
    /// 整数/浮点数和二进制、八进制、十六进制之间的转换
    /// </summary>
    public static class DataConvert
    {

        /// <summary>
        /// 获取正确长度的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="rightLength"></param>
        /// <returns></returns>
        private static string GetRightLengthString(string str, int rightLength)
        {
            string endStr = string.Empty;
            int strLength = str.Length;
            if (strLength <= rightLength)
            {
                endStr = str.PadLeft(rightLength, '0');
            }
            else //超出长度去掉右边多余长度
            {
                int startIndex = strLength - rightLength;
                endStr = str.Substring(startIndex, rightLength);
            }
            return endStr;
        }

        /// <summary>
        /// 转换大小写
        /// </summary>
        /// <param name="oriStr"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        private static string IsUpperOrLower(string oriStr, bool isUpper)
        {
            return isUpper ? oriStr.ToUpper() : oriStr.ToLower();
        }



        #region 整数转二进制字符串

        /// <summary>
        /// Byte转二进制字符串
        /// </summary>
        /// <param name="val">Byte类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertByteToBinaryStr(Byte val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 8);
        }

        /// <summary>
        /// SByte转二进制字符串
        /// </summary>
        /// <param name="val">SByte类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertSByteToBinaryStr(SByte val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 8);
        }



        /// <summary>
        ///  UInt16转二进制字符串
        /// </summary>
        /// <param name="val">UInt16类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertUInt16ToBinaryStr(UInt16 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 16);
        }

        /// <summary>
        ///  Int16转二进制字符串
        /// </summary>
        /// <param name="val">Int16类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertInt16ToBinaryStr(Int16 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 16);
        }



        /// <summary>
        ///  UInt32转二进制字符串
        /// </summary>
        /// <param name="val">UInt32类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertUInt32ToBinaryStr(UInt32 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 32);
        }


        /// <summary>
        ///  Int32转二进制字符串
        /// </summary>
        /// <param name="val">Int32类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertInt32ToBinaryStr(Int32 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 32);
        }


        /// <summary>
        /// UInt64转二进制字符串
        /// </summary>
        /// <param name="val">UInt64类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertUInt64ToBinaryStr(UInt64 val, bool isUpper = true)
        {
            ulong quotient = val;//商
            byte restValue = 0;//余数
            List<string> restValueList = new List<string>();
            do
            {
                restValue = (byte)(quotient % 2);//求除2后的余数
                string restStr = restValue.ToString();
                restValueList.Add(restStr);//将每次得到的余数放入集合
                quotient = quotient / 2;//求除2后的商
            }
            while (quotient > 0);
            restValueList.Reverse();
            string oriStr = string.Concat(restValueList);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 64);//模二除法
        }




        /// <summary>
        ///  Int64转二进制字符串
        /// </summary>
        /// <param name="val">Int64类型的值</param>
        /// <param name="isUpper">是否大写</param>
        /// <returns>二进制字符串</returns>
        public static string ConvertInt64ToBinaryStr(Int64 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 2);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 64);
        }

        #endregion

        #region 整数转八进制字符串

        /// <summary>
        ///  Byte转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertByteToOctalStr(byte val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 3);
        }

        /// <summary>
        ///  SByte转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertSByteToOctalStr(SByte val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 3);
        }

        /// <summary>
        ///  Int16转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertInt16ToOctalStr(Int16 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 6);
        }

        /// <summary>
        ///  UInt16转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertUInt16ToOctalStr(UInt16 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 6);
        }

        /// <summary>
        ///  UInt32转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertUInt32ToOctalStr(UInt32 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 12);
        }

        /// <summary>
        ///  Int32转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertInt32ToOctalStr(Int32 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 12);
        }

        /// <summary>
        ///  UInt64转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertUInt64ToOctalStr(UInt64 val, bool isUpper = true)
        {
            ulong quotient = val;//商
            byte restValue = 0;//余数
            List<string> restValueList = new List<string>();
            do
            {
                restValue = (byte)(quotient % 8);//求除8后的余数
                string restStr = restValue.ToString();
                restValueList.Add(restStr);//将每次得到的余数放入集合
                quotient = quotient / 8;//求除8后的商
            }
            while (quotient > 0);
            restValueList.Reverse();
            string oriStr = string.Concat(restValueList);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 24);
        }

        /// <summary>
        ///  Int64转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertInt64ToOctalStr(Int64 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 8);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 24);
        }




        #endregion

        #region 整数转十六进制字符串







        /// <summary>
        /// byte转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertByteToHexStr(byte val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 2);
        }

        /// <summary>
        /// SByte转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertSByteToHexStr(sbyte val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 2);
        }



        /// <summary>
        /// UInt16转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertUint16ToHexStr(UInt16 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 4);
        }

        /// <summary>
        /// UInt16转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertInt16ToHexStr(Int16 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 4);
        }

        /// <summary>
        /// UInt32转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertUInt32ToHexStr(UInt32 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 8);
        }

        /// <summary>
        /// Int32转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertInt32ToHexStr(Int32 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 8);
        }


        /// <summary>
        /// UInt64转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertUInt64ToHexStr(UInt64 val, bool isUpper = true)
        {
            ulong quotient = val;//商
            byte restValue = 0;//余数
            List<string> restValueList = new List<string>();
            do
            {
                restValue = (byte)(quotient % 16);//求除16后的余数

                string restStr = string.Empty;
                if (restValue < 10)
                {
                    restStr = restValue.ToString();
                }
                else
                {
                    switch (restValue)
                    {
                        case 10:
                            restStr = "A";
                            break;
                        case 11:
                            restStr = "B";
                            break;
                        case 12:
                            restStr = "C";
                            break;
                        case 13:
                            restStr = "D";
                            break;
                        case 14:
                            restStr = "E";
                            break;
                        case 15:
                            restStr = "F";
                            break;
                    }
                }
                restValueList.Add(restStr);//将每次得到的余数放入集合
                quotient = quotient / 16;//求除16后的商
            }
            while (quotient > 0);
            restValueList.Reverse();
            string oriStr = string.Concat(restValueList);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 16);
        }

        /// <summary>
        /// Int64转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertInt64ToHexStr(Int64 val, bool isUpper = true)
        {
            string oriStr = Convert.ToString(val, 16);
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 16);
        }


        #endregion

        #region 浮点数转二进制



        /// <summary>
        /// float转二进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertFloat32ToBinaryStr(float val, bool isUpper = true)
        {
            byte[] bytes = BitConverter.GetBytes(val);
            StringBuilder sb = new StringBuilder();
            int count = bytes.Length;
            // 字节数组从后取，因为BitConverter.GetBytes是小端序
            for (int i = count - 1; i >= 0; i--)
            {
                string biaryString = Convert.ToString(bytes[i], 2).PadLeft(8, '0');
                sb.Append(biaryString);
            }
            string oriStr = sb.ToString();
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 32);
        }

        /// <summary>
        /// double转二进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        public static string ConvertFloat64ToBinaryStr(double val, bool isUpper = true)
        {
            byte[] bytes = BitConverter.GetBytes(val);
            StringBuilder sb = new StringBuilder();
            int count = bytes.Length;
            for (int i = count - 1; i >= 0; i--)
            {
                string biaryString = Convert.ToString(bytes[i], 2).PadLeft(8, '0');
                sb.Append(biaryString);
            }
            string oriStr = sb.ToString();
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 64);
        }


        #endregion

        #region 浮点数转八进制



        /// <summary>
        /// float转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertFloat32ToOctalStr(float val, bool isUpper = true)
        {
            byte[] bytes = BitConverter.GetBytes(val);

            StringBuilder sb = new StringBuilder();
            int count = bytes.Length;
            for (int i = count - 1; i >= 0; i--)
            {
                string biaryString = Convert.ToString(bytes[i], 8).PadLeft(3, '0');
                sb.Append(biaryString);
            }
            string oriStr = sb.ToString();
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 12);
        }

        /// <summary>
        /// double转八进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertFloat64ToOctalStr(double val, bool isUpper = true)
        {
            byte[] bytes = BitConverter.GetBytes(val);
            StringBuilder sb = new StringBuilder();
            int count = bytes.Length;
            for (int i = count - 1; i >= 0; i--)
            {
                string biaryString = Convert.ToString(bytes[i], 8).PadLeft(3, '0');
                sb.Append(biaryString);
            }
            string oriStr = sb.ToString();
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 24);
        }
        #endregion

        #region 浮点数转十六进制字符串



        /// <summary>
        /// float转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertFloat32ToHexStr(float val, bool isUpper = true)
        {
            byte[] bytes = BitConverter.GetBytes(val);
            int count = bytes.Length;
            StringBuilder sb = new StringBuilder();
            for (int i = count - 1; i >= 0; i--)
            {
                string str = Convert.ToString(bytes[i], 16).ToUpper().PadLeft(2, '0');
                sb.Append(str);
            }
            string oriStr = sb.ToString();
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 8);
        }


        /// <summary>
        /// double转十六进制字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="isUpper"></param>
        /// <returns></returns>
        public static string ConvertFloat64ToHexStr(double val, bool isUpper = true)
        {
            byte[] bytes = BitConverter.GetBytes(val);
            int count = bytes.Length;
            StringBuilder sb = new StringBuilder();
            for (int i = count - 1; i >= 0; i--)
            {
                string str = Convert.ToString(bytes[i], 16).ToUpper().PadLeft(2, '0');
                sb.Append(str);
            }
            string oriStr = sb.ToString();
            string tempStr = IsUpperOrLower(oriStr, isUpper);
            return GetRightLengthString(tempStr, 16);
        }

        #endregion

        #region 字符串转整数

        #region 二进制字符串转整数






        /// <summary>
        /// 二进制字符串转Byte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns>byte</returns>
        public static byte ConvertBinaryStrToByte(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            byte endVal = 0;
            try
            {
                endVal = Convert.ToByte(str, 2);  // 将字符串转换为2进制，前提是字符串是二进制表示的方式,否则异常
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 二进制字符串转SByte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns>SByte</returns>
        public static sbyte ConvertBinaryStrToSByte(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            sbyte endVal = 0;
            try
            {
                endVal = Convert.ToSByte(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 二进制字符串转Int16
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int16 ConvertBinaryStrToInt16(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int16 endVal = 0;
            try
            {
                endVal = Convert.ToInt16(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 二进制字符串转UInt16
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt16 ConvertBinaryStrToUInt16(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt16 endVal = 0;
            try
            {
                endVal = Convert.ToUInt16(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 二进制字符串转Int32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int32 ConvertBinaryStrToInt32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int32 endVal = 0;
            try
            {
                endVal = Convert.ToInt32(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 二进制字符串转UInt32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt32 ConvertBinaryStrToUInt32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt32 endVal = 0;
            try
            {
                endVal = Convert.ToUInt32(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 二进制字符串转Int64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int64 ConvertBinaryStrToInt64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int64 endVal = 0;
            try
            {
                endVal = Convert.ToInt64(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 二进制字符串转UInt64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt64 ConvertBinaryStrToUInt64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt64 endVal = 0;
            try
            {
                endVal = Convert.ToUInt64(str, 2);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }


        #endregion

        #region 八进制字符串转整数
        /// <summary>
        /// 八进制字符串转Byte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns>byte</returns>
        public static byte ConvertOctalStrToByte(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            byte endVal = 0;
            try
            {
                endVal = Convert.ToByte(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 八进制字符串转SByte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns>SByte</returns>
        public static sbyte ConvertOctalStrToSByte(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            sbyte endVal = 0;
            try
            {
                endVal = Convert.ToSByte(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 八进制字符串转Int16
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int16 ConvertOctalStrToInt16(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int16 endVal = 0;
            try
            {
                endVal = Convert.ToInt16(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 八进制字符串转UInt16
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt16 ConvertOctalStrToUInt16(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt16 endVal = 0;
            try
            {
                endVal = Convert.ToUInt16(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 八进制字符串转Int32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int32 ConvertOctalStrToInt32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int32 endVal = 0;
            try
            {
                endVal = Convert.ToInt32(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 八进制字符串转UInt32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt32 ConvertOctalStrToUInt32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt32 endVal = 0;
            try
            {
                endVal = Convert.ToUInt32(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 八制字符串转Int64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int64 ConvertOctalStrToInt64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int64 endVal = 0;
            try
            {
                endVal = Convert.ToInt64(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 八进制字符串转UInt64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt64 ConvertOctalStrToUInt64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt64 endVal = 0;
            try
            {
                endVal = Convert.ToUInt64(str, 8);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }


        #endregion

        #region 十六进制字符串转整数
        /// <summary>
        /// 十六进制字符串转Byte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static byte ConvertHexStrToByte(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            byte endByte = 0;
            try
            {
                endByte = Convert.ToByte(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endByte;

        }
        /// <summary>
        ///  十六进制字符串转sByte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static sbyte ConvertHexStrToSByte(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            sbyte endVal = 0;
            try
            {
                endVal = Convert.ToSByte(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 十六进制字符串转Int16
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int16 ConvertHexStrToInt16(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int16 endVal = 0;
            try
            {
                endVal = Convert.ToInt16(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 十六进制字符串转UInt16
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt16 ConvertHexStrToUInt16(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt16 endVal = 0;
            try
            {
                endVal = Convert.ToUInt16(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 十六进制字符串转Int32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int32 ConvertHexStrToInt32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int32 endVal = 0;
            try
            {
                endVal = Convert.ToInt32(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        /// <summary>
        /// 十六进制字符串转UInt32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt32 ConvertHexStrToUInt32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt32 endVal = 0;
            try
            {
                endVal = Convert.ToUInt32(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 十六进制字符串转Int64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static Int64 ConvertHexStrToInt64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            Int64 endVal = 0;
            try
            {
                endVal = Convert.ToInt64(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 十六进制字符串转UInt64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static UInt64 ConvertHexStrToUInt64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            UInt64 endVal = 0;
            try
            {
                endVal = Convert.ToUInt64(str, 16);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        #endregion

        #endregion

        #region 字符串转浮点数


        #region 二进制字符串转浮点数

        /// <summary>
        /// 二进制字符串转Float32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static float ConvertBinaryStrToFloat32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            float endVal = 0;
            try
            {
                string tempStr = str.PadLeft(32, '0');
                List<byte> byteList = new List<byte>();
                for (int i = 0; i < 4; i++)
                {
                    int startIndex = i * 8;
                    string byteBinaryStr = tempStr.Substring(startIndex, 8);
                    byte val = Convert.ToByte(byteBinaryStr, 2);
                    byteList.Add(val);
                }
                byteList.Reverse();
                endVal = BitConverter.ToSingle(byteList.ToArray(), 0);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        /// <summary>
        /// 二进制字符串转Float64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static double ConvertBinaryStrToFloat64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            double endVal = 0;
            try
            {
                string tempStr = str.PadLeft(64, '0');
                List<byte> byteList = new List<byte>();
                for (int i = 0; i < 8; i++)
                {
                    int startIndex = i * 8;
                    string byteBinaryStr = tempStr.Substring(startIndex, 8);
                    byte val = Convert.ToByte(byteBinaryStr, 2);
                    byteList.Add(val);
                }
                byteList.Reverse();
                endVal = BitConverter.ToDouble(byteList.ToArray(), 0);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }

        #endregion




        #region 八进制字符串转浮点数
        /// <summary>
        /// 八进制字符串转float32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static float ConvertOctalStrToFloat32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            float endVal = 0;
            try
            {
                string tempStr = str.PadLeft(12, '0');
                List<byte> byteList = new List<byte>();
                for (int i = 0; i < 4; i++)
                {
                    int startIndex = i * 3;
                    string byteBinaryStr = tempStr.Substring(startIndex, 3);
                    byte val = Convert.ToByte(byteBinaryStr, 8);
                    byteList.Add(val);
                }
                byteList.Reverse();
                endVal = BitConverter.ToSingle(byteList.ToArray(), 0);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }


        /// <summary>
        /// 八进制字符串转float64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static double ConvertOctalStrToFloat64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            double endVal = 0;
            try
            {
                string tempStr = str.PadLeft(24, '0');
                List<byte> byteList = new List<byte>();
                for (int i = 0; i < 8; i++)
                {
                    int startIndex = i * 3;
                    string byteBinaryStr = tempStr.Substring(startIndex, 3);
                    byte val = Convert.ToByte(byteBinaryStr, 8);
                    byteList.Add(val);
                }
                byteList.Reverse();
                endVal = BitConverter.ToDouble(byteList.ToArray(), 0);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }


        #endregion


        #region 十六进制字符串转浮点数
        /// <summary>
        /// 十六进制字符串转Float32
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static float ConvertHexStrToFloat32(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            float endVal = 0;
            try
            {
                string tempStr = str.PadLeft(8, '0');
                List<byte> byteList = new List<byte>();
                for (int i = 0; i < 4; i++)
                {
                    int startIndex = i * 2;
                    string byteBinaryStr = tempStr.Substring(startIndex, 2);
                    byte val = Convert.ToByte(byteBinaryStr, 16);
                    byteList.Add(val);
                }
                byteList.Reverse();
                endVal = BitConverter.ToSingle(byteList.ToArray(), 0);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }



        /// <summary>
        /// 十六进制字符串转Float64
        /// </summary>
        /// <param name="str"></param>
        /// <param name="result"></param>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public static double ConvertHexStrToFloat64(string str, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            double endVal = 0;
            try
            {
                string tempStr = str.PadLeft(16, '0');
                List<byte> byteList = new List<byte>();
                for (int i = 0; i < 8; i++)
                {
                    int startIndex = i * 2;
                    string byteBinaryStr = tempStr.Substring(startIndex, 2);
                    byte val = Convert.ToByte(byteBinaryStr, 16);
                    byteList.Add(val);
                }
                byteList.Reverse();
                endVal = BitConverter.ToDouble(byteList.ToArray(), 0);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return endVal;
        }
        #endregion



        #endregion




        #region  字符串数组转整数数组
        /// <summary>
        /// 十六进制字符串数组转Byte数组
        /// </summary>
        /// <param name="strArray">输入的字符串数组</param>
        /// <param name="result">输出的转换结果true/false</param>
        /// <param name="errorStr">输出的转换信息</param>
        /// <returns></returns>
        public static byte[] ConvertHexStringArraytrToByteArray(string[] strArray, out bool result, out string errorStr)
        {
            errorStr = string.Empty;
            result = false;
            List<byte> listBytes = new List<byte>();

            try
            {
                foreach (string item in strArray)
                {
                    byte tempByte = Convert.ToByte(item, 16);
                    listBytes.Add(tempByte);
                }
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return listBytes.ToArray();
        }
        #endregion

        #region 整数数组转字符串数组
        /// <summary>
        /// 将字节数组转换成十六进制字符串
        /// </summary>
        /// <param name="byteArray"> 输入的字节数组</param>
        /// <param name="result"> 输出的结果true/false</param>
        /// <param name="errorStr">输出的信息</param>
        /// <param name="splitSt">分隔十六进制字符串的的分隔符</param>
        /// <param name="isUpper">是否转换为大写的十六进制字符串</param>
        /// <returns></returns>
        public static string ConvertByteArrayToHexStringArray(byte[] byteArray, out bool result, out string errorStr, string splitSt = " ", bool isUpper = true)
        {
            errorStr = string.Empty;
            result = false;
            string resultStr = string.Empty;
            List<string> listStr = new List<string>();

            try
            {
                foreach (byte item in byteArray)
                {
                    string str = string.Empty;
                    if (isUpper)
                    {
                        str = Convert.ToString(item, 16).ToUpper().PadLeft(2, '0');
                    }
                    else
                    {
                        str = Convert.ToString(item, 16).ToLower().PadLeft(2, '0');
                    }
                    listStr.Add(str);
                }
                if (string.IsNullOrEmpty(splitSt))//如果为空或者null，则转换成""
                {
                    splitSt = "";
                }
                resultStr = string.Join(splitSt, listStr);
                result = true;
            }
            catch (Exception ex)
            {
                errorStr = ex.Message;
            }
            return resultStr;
        }
        #endregion 

    }


}
